// user/pages/invoce/add.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dutyParagraph: '',
    companyName: '',
    email: '',
    phone:'',
    type: 1,
    more:true,
    id: '',
    // info: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.setData({
        id: options.id
      })
      this.fetchData()
    }
  },
  moreBind(){
    this.setData({
      more: !this.data.more
    })
  },
  changeType(e){
    console.log(e)
    let type = e.currentTarget.dataset.type
    this.setData({
      type: type
    })
  },
  // inputSet(e){
  //   console.log(e)
  // },
  fetchData(){
    let this_ = this
    service('POST', `/invoicing/info/template/${this.data.id}`).then((res) => {
      let data = res.data.template
      if(data.id){
        let type = data.dutyParagraph ? 1: 2
          this_.setData({
            companyName: data.companyName,
            dutyParagraph: data.dutyParagraph,
            email: data.email,
            phone: data.phone,
            type: type
         })
      }
      
    })
  },
  addForm(){
    let companyName = this.data.companyName 
    var dutyParagraph = this.data.dutyParagraph 
    let email = this.data.email 
    let phone = this.data.phone 
    let address =this.data.address 
    let bankName =this.data.bankName 
    let accountNumber =this.data.accountNumber
    let type = this.data.type
    
    if(type==1){
      if(!companyName){this.message('请输入发票抬头')
       return}
       if(!dutyParagraph){this.message('请输入税号')
       return}
    }else{
      if(!companyName){this.message('请输入姓名')
       return}
       if(dutyParagraph){dutyParagraph =''}
    }
    var emailReg = /^\w{3,}(\.\w+)*@[A-z0-9]+(\.[A-z]{2,5}){1,2}$/
    if(!emailReg.test(email)){
      this.message('请输入正确的邮箱号')
       return
    }
    if(!/^1[3-9]\d{9}$/.test(phone)){
      this.message('请输入正确的手机号')
       return
    }
    let id = this.data.id
    let url = id? '/invoicing/updat/template': '/invoicing/company/template'
    let postData ={companyName, dutyParagraph, accountNumber, email, phone, accountNumber, address, bankName}
    if(id){
      postData = {...postData , id}
    }
    // console.log(postData)
    service('POST', url, postData).then((res) => {
      if(res.data.code==0){
        wx.navigateBack()
      }else{
      }
      
    })
  },
  message(msg){
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})